<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

defined ( '_JEXEC' ) or die ();

jimport ( 'joomla.application.component.view' );

class JobBoardViewJAFeeds extends JView {
	function display($tpl = null) {
		global $jbconfig,$mainframe;
        if (!$jbconfig['feeds']->get('show_feed', 1))
        {
        	return $mainframe->redirect(JRoute::_("index.php?option=com_jajobboard&view=jaerrors&Itemid=".JRequest::getVar("Itemid")),JText::_('Sorry, currently our system does not support RSS'));			
        	
        }
        
        $title = getPageTitleFromMenu();
    	$config = new JConfig();
		$document = JFactory::getDocument();
		
		switch ($this->getLayout()) {
			case 'guide' :
				$this->getItemList ();
				
				if (!$title)
					$title = JText::_('Feeds Guide');
				$document->setTitle($title.' - '.$config->sitename);
				
				break;
			case 'form':
				$this->displayForm();
				
				if (!$title)
					$title = JText::_('Create new a feed');
				$document->setTitle($title.' - '.$config->sitename);
				
				break;
			case 'feed_link':
				$feed =& $this->get('Item');
				$this->assignRef('row', $feed);				
				if (!$title)
					$title = JText::_('Feeds Page');
				$document->setTitle($title.' - '.$config->sitename);
				break;
			case 'rss' :				
				$feed = getItemByFieldName('ja_feeds','feed_alias',"'".JRequest::getVar('alias')."'");
				if (isset($feed[0]))
				{
					$feed = $feed[0];
					
					$row = $feed;
					if (($row->filter_cat_id!='')&&($row->filter_cat_id!=null))
					{
						$db = JFactory::getDBO();
						$query = "SELECT  name FROM #__ja_categories WHERE id IN ($row->filter_cat_id)";
						$db->setQuery($query);
						$result = $db->loadResultArray();
						if (is_array($result))
//							$str_filter .= "&category=".implode(',',$result);	
							JRequest::setVar('category',implode(',',$result));
					}				
					if (($row->filter_location_id!='')&&($row->filter_location_id!=null))
					{	
						$db = JFactory::getDBO();
						$query = "SELECT  name FROM #__ja_location WHERE id IN ($row->filter_location_id)";
						$db->setQuery($query);
						$result = $db->loadResultArray();
						if (is_array($result))
//							$str_filter .= "&location=".implode(',',$result);	
							JRequest::setVar('location',implode(',',$result));
					}
					$content = & $this->get ( 'Content' );
				}
				else
				{
					$content = & $this->get ( 'Content' );
					$feed = & $this->get ( 'Item' );
					$urlparams = $this->get ( 'URLParams' );
					$feed->feed_name = isset ( $urlparams ['name'] ) ? $urlparams ['name'] : $feed->feed_name;
					$feed->feed_type = $urlparams ['type'] ? $urlparams ['type'] : $feed->feed_type;
					
					$feed->msg_count = $urlparams ['job_number'] ? $urlparams ['job_number'] : $feed->msg_count;
					$feed->msg_orderby = $urlparams ['order_by'] ? $urlparams ['order_by'] : $feed->msg_orderby;
					$feed->feed_cache = 0;
					$feed->feed_description = $urlparams ['description'] ? $urlparams ['description'] : $feed->feed_description;
					
				}
				
				$menuItemArray = & $this->get ( 'MenuItemArray' );
				$this->assignRef ( 'id', $feed->id );
				$this->assignRef ( 'title', $feed->feed_name );
				$this->assignRef ( 'type', $feed->feed_type );
				$this->assignRef ( 'count', $feed->msg_count );
				$this->assignRef ( 'orderby', $feed->msg_orderby );
				$this->assignRef ( 'cache', $feed->feed_cache );
				$this->assignRef ( 'description', $feed->feed_description );
				$this->assignRef ( 'renderAuthorFormat', $feed->feed_renderAuthorFormat );
				$this->assignRef ( 'numWords', $feed->msg_numWords );
				$this->assignRef ( 'feed_alias', $feed->feed_alias);
				$this->assignRef ( 'imgUrl', $feed->feed_imgUrl );
				$this->assignRef ( 'content', $content );
				$this->assignRef ( 'menuitemarray', $menuItemArray );
				$this->setLayout('rss');
		}
		parent::display ( $tpl );
	}
	function getItemList() {
		global $mainframe, $option;
		$items = & $this->get ( 'Items' );
		$pagination = & $this->get ( 'Pagination' );
		
		$this->assignRef ( 'items', $items );
		$this->assignRef ( 'pagination', $pagination );
	}
	
  	function displayForm(){  	    
  		global $option,$jbconfig;
  		$this->setLayout('form');
  		$feed =& $this->get('Item');
  		if (JRequest::getVar('postback',false))
  		{
  			$post    = JRequest::get('request', JREQUEST_ALLOWHTML);
            $feed->bind($post);
  		}
		$isNew = ($feed->id<1);
		
		
		$lists = array();
		
		if ($isNew)  {
			$default =& $this->get('DefaultData');			
		} else {
			// for existing items the button is renamed `close`
			
		}
		
		
		//rss type list
		$rssType[] = JHTML::_('select.option', '0.91','RSS 0.91');
		$rssType[] = JHTML::_('select.option', '1.0','RSS 1.0');
		$rssType[] = JHTML::_('select.option', '2.0','RSS 2.0');
		$rssType[] = JHTML::_('select.option', "MBOX","MBOX");
		$rssType[] = JHTML::_('select.option', "OPML","OPML");
		$rssType[] = JHTML::_('select.option', "ATOM","ATOM");
		$rssType[] = JHTML::_('select.option', "ATOM0.3","ATOM 0.3");
		$rssType[] = JHTML::_('select.option', "HTML","HTML");
		$rssType[] = JHTML::_('select.option', "JS","JS");
		$lists['rssTypeList'] = JHTML::_('select.genericlist', $rssType, 'feed_type', 'class="inputbox"', 'value', 'text', $isNew ? $jbconfig['feeds']->get('defaultType') : $feed->feed_type, 'feed_type');

		$fulltext[] = JHTML::_('select.option', "0","Do nothing");
		$fulltext[] = JHTML::_('select.option', "1","Read more link");
		$fulltext[] = JHTML::_('select.option', "2","Add to intro text");
		$lists['fulltextlist'] = JHTML::_('select.genericlist', $fulltext, 'msg_fulltext', 'class="inputbox"', 'value', 'text',  $isNew ? '1': $feed->msg_fulltext );
		
		$orderings[] = JHTML::_('select.option', 'date','Date Ascending');
		$orderings[] = JHTML::_('select.option', 'rdate','Date Descending');
		$lists['orderingList'] = JHTML::_('select.genericlist', $orderings, 'msg_orderby', 'class="inputbox"', 'value', 'text', $isNew ? $jbconfig['feeds']->get('orderby') : $feed->msg_orderby, 'msg_orderby');
		
		$numWords[] = JHTML::_('select.option','0','All');
		for ($i=25;$i<=250;$i+=25) {
			$numWords[] = JHTML::_('select.option',$i,$i);
		}
		$lists['numWordsList'] = JHTML::_('select.genericList', $numWords, 'msg_numWords', 'class="inputbox"','value', 'text', $isNew ? $jbconfig['feeds']->get('numWords') : $feed->msg_numWords, 'msg_numWords' );
		
		$authorformats[] = JHTML::_( 'select.option', 'NAME','Name Only');
		$authorformats[] = JHTML::_( 'select.option', 'EMAIL','Email Only');
		$authorformats[] = JHTML::_( 'select.option', 'NAME&EMAIL','Name and Email');
		$lists['renderAuthorList'] = JHTML::_('select.genericList', $authorformats, 'feed_renderAuthorFormat', 'class="inputbox"','value', 'text', $isNew ? $jbconfig['feeds']->get('renderAuthorFormat') : $feed->feed_renderAuthorFormat, 'feed_renderAuthorFormat' );
		
		$renderHTML[] = JHTML::_( 'select.option', '1','Yes');
		$renderHTML[] = JHTML::_( 'select.option', '0','No');
		$lists['renderHTMLList'] =JHTML::_( 'select.genericList',$renderHTML, 'feed_renderHTML', 'class="inputbox"','value', 'text', $isNew ? $jbconfig['feeds']->get('renderHTML') : $feed->feed_renderHTML , 'feed_renderHTML');
		
		
		$renderImages[]   = JHTML::_( 'select.option', "1","Yes");
		$renderImages[]   = JHTML::_( 'select.option', "0","No");
		$lists['renderImagesList'] = JHTML::_( 'select.genericList', $renderImages, 'feed_renderImages', 'class="inputbox"','value', 'text',$isNew ? '1' : $feed->feed_renderImages );
	
		$renderPubl[] = JHTML::_( 'select.option', "1","Yes");
		$renderPubl[] = JHTML::_( 'select.option', "0","No");
		$lists['renderPublishedList'] = JHTML::_( 'select.genericList', $renderPubl, 'published', 'class="inputbox"','value', 'text',$isNew ? NULL : $feed->published);
//		Filter feeds - begin -		
		$value_Location = getItemByFieldName('ja_location','','');
		$option_Location = array();
		$option_Location[] = JHTML::_('select.option','',JText::_('All'));
		if($isNew)
			$locationSelected = '';
		else
			if (!is_array($feed->filter_location_id))
				$locationSelected = explode(',',$feed->filter_location_id);
			else 
				$locationSelected = $feed->filter_location_id;
		if (is_array($value_Location))
		{
			foreach ($value_Location as $location)
				$option_Location[] = JHTML::_('select.option',$location->id,$location->name);
		}
		$lists['filter_location_id'] = JHTML::_( 'select.genericList',$option_Location, 'filter_location_id' . '[]', ' style="width:200px" size="10" class="inputbox"  multiple="true"', 'value', 'text', $locationSelected );
		
		$value_cat = getItemByFieldName('ja_categories','','');
		$option_Cat = array();
		$option_Cat[] = JHTML::_('select.option','',JText::_('All'));
		if($isNew)
			$catSelected = '';
		else
			if (!is_array($feed->filter_cat_id))
				$catSelected = explode(',',$feed->filter_cat_id);
			else 
				$catSelected = $feed->filter_cat_id;
		if (is_array($value_cat))
		{
			foreach ($value_cat as $cat)
				$option_Cat[] = JHTML::_('select.option',$cat->id,$cat->name);
		}
		$lists['filter_cat_id'] = JHTML::_( 'select.genericList',$option_Cat, 'filter_cat_id' . '[]', ' style="width:200px" size="10" class="inputbox"  multiple="true"', 'value', 'text', $catSelected );
		
//		Filter feeds - end -
		
		
		//Feedbutton images uit de directory laden
		$button_path = JPATH_ROOT .DS. "components".DS."com_jajobboard".DS."views".DS."jafeeds".DS."images".DS."buttons";
		$dir = @opendir($button_path);
		$button_images = array();
		$button_col_count = 0;

		while( $file = @readdir($dir) )
		{
			if( $file != '.' && $file != '..' && is_file($button_path . '/' . $file) && !is_link($button_path . '/' . $file) )
			{
				if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $file) )
				{
				   $button_images[$button_col_count] = $file;
				   $button_name[$button_col_count] = ucfirst(str_replace("_", " ", preg_replace('/^(.*)\..*$/', '\1', $file)));
				   $buttons[] = JHTML::_( 'select.option', $button_images[$button_col_count], $button_name[$button_col_count]);
				   $button_col_count++;				
				}
			}
		}
		@closedir($dir);
		$lists['feedButtons'] = JHTML::_( 'select.genericList', $buttons, 'feed_button', 'onchange="loadButton(this)" class="inputbox" ','value', 'text',$isNew ? 'rss20.gif' : $feed->feed_button);
		
		//Editor
//		$editor  =& JFactory::getEditor();	
		
		$this->assignRef('is_user', $feed->is_user);
		$this->assignRef('id', $feed->id);
		$this->assignRef('name', $feed->feed_name);
		$this->assignRef('alias', $feed->feed_alias);
		$this->assignRef('count', $feed->msg_count = $isNew? $jbconfig['feeds']->get('count'):$feed->msg_count);
		$this->assignRef('cache', $feed->feed_cache = $isNew? $jbconfig['feeds']->get('cache'):$feed->feed_cache);
		$this->assignRef('imgUrl', $feed->feed_imgUrl);
		$isNew? $feed->feed_button='rss20.gif':$feed->feed_button;
		$this->assignRef('BtnImgUrl', $feed->feed_button);
		$this->assignRef('exitems', $feed->filter_exitems);
		$this->assignRef('exemployers', $feed->filter_exemployers);
		$this->assignRef('inemployers', $feed->filter_inemployers);
		$this->assignRef('description', $feed->feed_description);
//		$this->assignRef('editor', $editor);
		$this->assignRef('lists', $lists);
		$this->assignRef('feed',$feed);
	
  	   
	}	
}
?>